package com.pinda.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinda.commonutils.Page;
import com.pinda.commonutils.Result;
import com.pinda.commonutils.StatusCode;
import com.pinda.oms.entity.PdOrder;
import com.pinda.oms.entity.PdTickupAndOrderAndUser;
import com.pinda.service.IPdTaskPickupDispatchService;
import com.pinda.work.entity.PdTaskPickupDispatch;
import com.pinda.work.entity.PdTickupAndOrder;
import com.pinda.work.vo.Dispatch_Order;
import com.pinda.work.vo.PdTaskPickupDispatchView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 取件、派件任务信息表 前端控制器
 * </p>
 *
 * @author 黄杰
 * @since 2023-06-13
 */
@RestController
@RequestMapping("/pd-task-pickup-dispatch")
public class PdTaskPickupDispatchController {

    @Autowired
    private IPdTaskPickupDispatchService  iPdTaskPickupDispatchService;


    /**
     * 派件任务
     * 待交接
     * 收件任务 1（待执行）
     */
    @GetMapping("findDispatchByTaskType")
    public Result<List<Dispatch_Order>> findDispatchByTaskType(String courierId) {
        System.out.println(courierId);
        List<Dispatch_Order> dispatchByTaskType = iPdTaskPickupDispatchService.findDispatchByTaskType(courierId);
        return Result.<List<Dispatch_Order>>builder().data(dispatchByTaskType).code(StatusCode.OK).flag(true).message("查询成功").build();
    }

    /**
     * 待妥投
     * @param courierId
     * @return
     */
    @GetMapping("findDispatchByTaskType3")
    public Result<List<Dispatch_Order>> findDispatchByTaskType3(String courierId) {
        List<Dispatch_Order> dispatchByTaskType = iPdTaskPickupDispatchService.findDispatchByTaskType3(courierId);
        return Result.<List<Dispatch_Order>>builder().data(dispatchByTaskType).code(StatusCode.OK).flag(true).message("查询成功").build();
    }

    /**
     * 查询待取件
     * @param courierid
     * @return
     */
    @GetMapping ("selectreceivingByid")
    public Result findDispatch(String courierid) {
        List<Dispatch_Order> selectreceiving = iPdTaskPickupDispatchService.selectreceiving(courierid);
        System.out.println(selectreceiving);
        return Result.builder().data(selectreceiving).code(StatusCode.OK).flag(true).message("查询成功").build();
    }

    /**
     * 查询须交件
     * @param courierid
     * @return
     */
    @GetMapping ("selectreceivingByid2")
    public Result findDispatch2(String courierid) {
        List<Dispatch_Order> dispatch_orders = iPdTaskPickupDispatchService.selectreceiving2(courierid);
        return Result.builder().data(dispatch_orders).code(StatusCode.OK).flag(true).message("查询成功").build();
    }

    /**
     * 查询已取消
     * @param courierid
     * @return
     */
    @GetMapping ("findcanceled")
    public Result findcanceled(String courierid) {
        List<Dispatch_Order> canceled = iPdTaskPickupDispatchService.canceled(courierid);
        return Result.builder().data(canceled).code(StatusCode.OK).flag(true).message("查询成功").build();
    }

    /**
     * 查询当前快递员的已完成任务或未完成任务
     */
    @PostMapping("findDispatchByCourierId")
    public Result<List<Dispatch_Order>> findDispatchByCourierId (@RequestBody PdTaskPickupDispatch pdTaskPickupDispatch){
        System.out.println(pdTaskPickupDispatch);
        List<Dispatch_Order> dispatchByCourierId = iPdTaskPickupDispatchService.findDispatchByCourierId(pdTaskPickupDispatch);
        return Result.<List<Dispatch_Order>>builder().data(dispatchByCourierId).message("查询成功").code(StatusCode.OK).build();
    }


//    /**
//     * 物流运费
//     * @param distance
//     * @param weight
//     * @return
//     */
//    @GetMapping("freight")
//    public Double freight ( double distance, double weight){
//        double Exp;
//        if (weight > 1) {
//            if (distance > 0 && distance < 200) {
//                Exp = (weight - 1) * 6 + 20;
//                return Exp;
//            }
//            if (distance >= 200 && distance <= 500) {
//                Exp = (weight - 1) * 9 + 20;
//                return Exp;
//            }
//            if (distance > 500) {
//                Exp = (weight - 1) * 15 + 20;
//                return Exp;
//            }
//        }
//        return 20.00;

//        KieServices kieServices = KieServices.Factory.get();
//        //获取kie容器,kmodule.xml文件中，kieBase指定的name(可以不指定)
//        KieContainer kieContainer = kieServices.newKieClasspathContainer();
//        //获取kiesession，kmoudle.xml文件中kieSeeeion属性name(可以不指定)
//        KieSession kieSession = kieContainer.newKieSession("simpleRuleKSession");
//        //事实对象
//        WuliuYunfei wuliuYunfei = new WuliuYunfei();
//        //设置征信登记为1
//        wuliuYunfei.setDistance(10.00);
//        wuliuYunfei.setWeight(10.00);
//        //插入事实对象到session中
//        kieSession.insert(wuliuYunfei);
//        //执行规则引擎
//        kieSession.fireAllRules();
//        System.out.println("对象信息：" + wuliuYunfei.getFreight());
//        return null;
//    }

    /**
     *收件任务
      * @param
     * @return
     */
    @PostMapping("/findPickupOne")
    public PdTickupAndOrderAndUser findOrderone(@RequestBody PdOrder pdOrder){
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
            return  iPdTaskPickupDispatchService.findOrderone(pdOrder.getId());
    }

    /**
     * 派件任务
     * @param pdOrder
     * @return
     */
    @PostMapping("/findPickuppaiOne")
    public PdTickupAndOrderAndUser findPickuppaiOne(@RequestBody PdOrder pdOrder){
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        return  iPdTaskPickupDispatchService.findPickuppaiOne(pdOrder.getId());
    }

    /**
     * 查询快递员（收）
     * @param page
     * @return
     */
    @PostMapping("/findTickupAndOrder")
    public PageInfo<PdTickupAndOrder> findTickupAndOrder(@RequestBody Page<PdTickupAndOrder> page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<PdTickupAndOrder> list = iPdTaskPickupDispatchService.findTickupAndOrder(page.getSearch());
        PageInfo<PdTickupAndOrder> pageInfo = new PageInfo(list);
        return pageInfo ;
    }

    /**
     * 查询快递员（派）
     * @param page
     * @return
     */
    @PostMapping("/findTickupAndOrderpai")
    public PageInfo<PdTickupAndOrder> findTickupAndOrderpai(@RequestBody Page<PdTickupAndOrder> page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<PdTickupAndOrder> list = iPdTaskPickupDispatchService.findTickupAndOrderpai(page.getSearch());
        PageInfo<PdTickupAndOrder> pageInfo = new PageInfo(list);
        return pageInfo ;
    }
    /**
     * 查询快递员（取消）
     * @param page
     * @return
     */
    @PostMapping("/findTickupAndOrderquxiao")
    public PageInfo<PdTickupAndOrder> findTickupAndOrderquxiao(@RequestBody Page page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<PdTickupAndOrder> list = iPdTaskPickupDispatchService.findTickupAndOrderquxiao();
        PageInfo<PdTickupAndOrder> pageInfo = new PageInfo(list);
        return pageInfo ;
    }



    /**
     * 查询取件和派件
     * @param val
     * @return
     */
    @GetMapping("/queryPdTaskPickupDispatchView/{id}")
    public List<PdTaskPickupDispatchView> queryPdTaskPickupDispatchViewallController(@PathVariable("id") String val){
        return this.iPdTaskPickupDispatchService.queryPdTaskPickupDispatchViewallService(val);
    }

}
